package com.platform.core.web.controller;

import com.platform.aspect.WebLog;
import com.platform.core.service.SysUserRoleService;
import com.platform.dto.RoleQo;
import com.platform.comm.web.results.JsonResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Description: 用户角色管理
 *
 * @author libin Created on 2019/7/22
 **/
@RestController
@RequestMapping("management/userRole")
public class SysUserRoleController {

    private final SysUserRoleService sysUserRoleService;

    public SysUserRoleController(SysUserRoleService sysUserRoleService) {
        this.sysUserRoleService = sysUserRoleService;
    }

    @PostMapping(name = "用户关联角色", path = "relateRoles")
    @WebLog(theme = "修改[用户关联角色]", moduleName = "用户角色管理", type = WebLog.OpType.MOD, root = Map.class)
    public JsonResult relateRoles(@RequestBody Map<String, Object> map) {
        JsonResult result = JsonResult.of();
        String userId = map.get("userId").toString();
        List<String> roleIds = (List<String>) map.get("roleIds");
        sysUserRoleService.create(userId, roleIds);
        result.setMessage("用户管理角色成功");
        return result.succeed();
    }

    @PostMapping(name = "根据用户名查询角色", path = "queryRolesByUserId")
    public List<RoleQo> queryRolesByUserId(String userId) {
        return sysUserRoleService.findRolesByUserId(userId);
    }

}
